import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:imperius_insights/portal/logger.dart';

import 'remote_callable.dart';

const DEFAULT_CONNECT_TIMEOUT = 1000 * 30;
const DEFAULT_READ_TIMEOUT = 1000 * 60;
const DEFAULT_SEND_TIMEOUT = 1000 * 30;

// open -n /Applications/Google\ Chrome.app/ --args --disable-web-security --user-data-dir=/Users/qitmac000936/Documents/somthing/cross
// http://qa.corp.qunar.com/qodin/api/issue/getPlan?param=%E6%9C%BA%E7%A5%A8%E4%BA%8B%E4%B8%9A%E9%83%A8%2C%E7%94%A8%E6%88%B7%E4%BA%A7%E5%93%81%E9%83%A8%2C%E6%8A%80%E6%9C%AF%2CAndroid&typew=&parent=1%2C2
// https://todolist.qunar.com/case/queryCaseCount.do?projectId=FLIGHT-108959
// http://findbug.qunar.com/statistics/form.do?moduleSelect=mudidi_rexian_wenti&type=todolist&dimension=Bug%E7%8A%B6%E6%80%81%E5%8F%8A%E4%BC%98%E5%85%88%E7%BA%A7%E6%95%B0%E9%87%8F%E7%BB%9F%E8%AE%A1&pmoNumber=FLIGHT-106623
//  问题类型占比：P1~P4、优化建议、UI展示及文案、非问题总数和占比
//  待解决数量：未处理/解决中
//  待验证数量：已解决/测试中
//  reopened：未解决
// http://qa.corp.qunar.com/qodin/api/easyPmoNoah/getPmoInfo?pmo=FLIGHT-111579
// http://qa.corp.qunar.com/qodin/api/easyPmo/getSteps?projectId=FLIGHT-114885
// flutter build apk --target-platform android-arm64 --split-per-abi
// http://100.80.193.63:9000/api/measures/search?projectKeys=q-design&metricKeys=alert_status,bugs,reliability_rating,vulnerabilities,security_rating,code_smells,sqale_rating,duplicated_lines_density,coverage,ncloc,ncloc_language_distribution
// http://100.80.193.63:9000/api/measures/component?additionalFields=metrics%2Cperiods&component=q-design&metricKeys=alert_status%2Cquality_gate_details%2Cbugs%2Cnew_bugs%2Creliability_rating%2Cnew_reliability_rating%2Cvulnerabilities%2Cnew_vulnerabilities%2Csecurity_rating%2Cnew_security_rating%2Ccode_smells%2Cnew_code_smells%2Csqale_rating%2Cnew_maintainability_rating%2Csqale_index%2Cnew_technical_debt%2Ccoverage%2Cnew_coverage%2Cnew_lines_to_cover%2Ctests%2Cduplicated_lines_density%2Cnew_duplicated_lines_density%2Cduplicated_blocks%2Cncloc%2Cncloc_language_distribution%2Cprojects%2Cnew_lines
const DEFAULT_HOST = 'https://www.microsoft.com';

class WebCallableTarget extends CallTarget {
  String host;
  String relativePath;

  WebCallableTarget({ this.host, this.relativePath });
}

class WebChannel extends RemoteCallable<WebCallableTarget> {
  Dio _dioInst;
  String _host;

  WebChannel({int connectTimeout = DEFAULT_CONNECT_TIMEOUT,
    int receiveTimeout = DEFAULT_READ_TIMEOUT,
    int sendTimeout = DEFAULT_SEND_TIMEOUT,
    String host = DEFAULT_HOST}) {
    BaseOptions options = new BaseOptions();
    options.connectTimeout = connectTimeout;
    options.receiveTimeout = receiveTimeout;
    options.sendTimeout = sendTimeout;
    _dioInst = new Dio(options);
    _host = host;
  }

  @override
  Future<Response> invokeForResponse(
      {@required WebCallableTarget target, Map<String, dynamic> params}) async {
    final host = target.host == null ? _host : target.host;
    final url = target.uri == null ? '$host${target.relativePath}' : target.uri.toString();
    try {
      return await _dioInst.get(url, queryParameters: params);
    } catch(err) {
      Logger.e(msg: 'invoke failure...$url, params=$params, err=$err, ${err..enresponse}');
      throw err;
    }
  }

  @override
  Future<String> invoke(
      {@required WebCallableTarget target, Map<String, dynamic> params}) async {
    final response = await invokeForResponse(target: target, params: params);
    return response.toString();
  }
}
